package jsyf.oa.service.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import jsyf.oa.dao.system.RoleRightConfigLogDao;
import jsyf.oa.entity.system.RoleRightConfigLogEntity;
import jsyf.oa.facade.system.RoleRightConfigLogFacade;

@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class RoleRightConfigLogService implements RoleRightConfigLogFacade {
    @Autowired
    private RoleRightConfigLogDao rolerightconfiglogDao;

    @Override
    public String save(String jsonData) {
        RoleRightConfigLogEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), RoleRightConfigLogEntity.class);
        rolerightconfiglogDao.add(entity);
        return entity.getId();
    }

    @Override
    public JSONObject getLogList(String jsonData, Integer requestPage, Integer pageSize) {
        //RoleRightConfigLogEntity entity = JSON.toJavaObject(JSON.parseObject(jsonData), RoleRightConfigLogEntity.class);

        StringBuilder sbCondition = new StringBuilder();
        sbCondition.append("");
        List<Object> sqlArgs = new ArrayList<Object>();

        List<JSONObject> records = rolerightconfiglogDao.getJSONListByPage(sbCondition.toString(),sqlArgs.toArray(), requestPage, pageSize);
        int recordCount = rolerightconfiglogDao.getPageCount(sbCondition.toString(), sqlArgs.toArray());
        int recordSize = Integer.valueOf(pageSize).intValue();
        int pageCount = recordCount % recordSize == 0 ? recordCount / recordSize : (recordCount / recordSize) + 1;

        JSONObject result = new JSONObject();
        JSONObject pagerObj = new JSONObject();
        pagerObj.put("recordCount", recordCount);
        pagerObj.put("pageCount", pageCount);
        pagerObj.put("requestPage", 1);
        result.put("pager", pagerObj);
        // 子数组对象
        result.put("records", records);
        return result;
    }

}
